package com.ruoyi.auth.controller;

import com.ruoyi.auth.form.LoginBody;
import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * token 控制
 *
 * @author ruoyi
 */
@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("findUsername")
    public R findUsername(@RequestBody LoginBody form) {
        String username = form.getUsername();
        String password = form.getPassword();
        LoginUser user = sysLoginService.login(username, password);
        if (user != null) {
            return R.ok("登录成功");
        } else {
            return R.fail("用户名或密码错误");
        }
    }

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form) {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody) {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }

//    @PostMapping("/sendMessage/{phoneNumber}")
//    public R<?> sendMessage(@PathVariable String phoneNumber) {
//        // 用户登录
//        String userInfo = sysLoginService.sendMessage(phoneNumber);
//        if (userInfo != null) {
//            // 假设 userInfo 是一个 JSON 字符串，需要解析为 LoginUser 对象
//            LoginUser loginUser = sysLoginService.parseUserInfo(userInfo);
//            if (loginUser != null) {
//                // 获取登录token
//                return R.ok(tokenService.createToken(loginUser));
//            } else {
//                return R.fail("解析用户信息失败");
//            }
//        } else {
//            return R.fail("用户不存在");
//        }
//    }

    @PostMapping("/sendMessage/{phoneNumber}")
    public String sendMessage(@PathVariable String phoneNumber) {
        return sysLoginService.sendMessage(phoneNumber);
    }
//    @PostMapping("/loginByPhoneCode")
//    public AjaxResult loginByPhoneCode(@RequestBody String phoneNumber, @RequestBody String code) {
//        return sysLoginService.loginByPhoneCode(phoneNumber, code);
//    }

}
